package com.example.baiduai.control;

import com.example.baiduai.dao.impl.GenericDaoImpl;
import com.example.baiduai.entity.JsonGenericBean;
import com.example.baiduai.tools.FileTools;
import com.example.baiduai.service.FaceBodyService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * @author wangyizhu
 * @version "1.8.0_131"
 * @email 2641956765@qq.com
 * @desc:该控制器类用于处理人脸与人体检测模块的前端请求
 * @Time 2022/09/22 9:43
 */

public class FaceBodyControl implements Controller {
    public void handle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1.设置编码
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //2获取前端获取的参数
        //如何读取以multipart/form-data形式传递的数据
        String type = req.getParameter("type");

        FaceBodyService faceBodyService = new FaceBodyService();
        PrintWriter writer = resp.getWriter();
        GenericDaoImpl genericDao=new GenericDaoImpl();
        switch (type) {
            case "face":

                byte[] faceData = FileTools.getFileDataByRequest(req);

                String s=faceData+"";
                System.out.println("图片数据:"+s);
                JsonGenericBean select = genericDao.select("人脸检测",s);
                if (select==null){
                    System.out.println("无数据");
                    String result = faceBodyService.face(faceData, "age,beauty,expression,eye_status,face_shape,emotion,face_type,glasses,gender");
                    genericDao.insert("人脸检测",s,result);
                    writer.write(result);
                }else {
                    System.out.println("有数据");
                    writer.write(select.getJson());
                }

                break;
            default:
            case "face_equals":
                byte[] faceData2 = FileTools.getFileDataByRequest(req);
                String result2 = faceBodyService.face_equals(faceData2);
                writer.write(result2);
                break;
        }
        writer.flush();
        writer.close();
    }


}
